package stdying.handyweibo.presenter;

import com.sina.weibo.sdk.auth.Oauth2AccessToken;

import javax.inject.Inject;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import stdying.handyweibo.model.User;
import stdying.handyweibo.rest.WeiboShowService;
import stdying.handyweibo.rest.WeiboUserService;
import stdying.handyweibo.view.IMainView;
import timber.log.Timber;

/**
 * Description: MainActivityPresenterModule
 * 主页逻辑
 * Author: liulili
 * Created: 2015-11-23
 */
public class MainActivityPresenter implements IPresenter{

    //view接口
    private IMainView mainView;
    //service访问
    private WeiboUserService weiboUserService;
    private WeiboShowService weiboShowService;

    //token信息
    private Oauth2AccessToken oauth2AccessToken;

    @Inject
    public MainActivityPresenter(IMainView mainView,Oauth2AccessToken oauth2AccessToken,WeiboUserService weiboUserService,WeiboShowService weiboShowService){
        this.mainView = mainView;
        this.weiboUserService = weiboUserService;
        this.oauth2AccessToken = oauth2AccessToken;
        this.weiboShowService = weiboShowService;
    }


    /**
     * 获取用户信息
     * http://open.weibo.com/wiki/2/users/show
     */
    public void getUserInfo(){
        Timber.e("获取用户信息");
        Subscription subWeibo = weiboUserService.getUserInfo(oauth2AccessToken.getToken(), oauth2AccessToken.getUid())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<User>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(User user) {
                        Timber.i(" --> user:id %s", user.getId());
                        mainView.refreshUserInfo();
                    }
                });

        //订阅集中管理
        mCompositeSubscription.add(subWeibo);
    }


    /**
     *
     */
    public void getLastMyFriends(){
        Timber.i(" --> 微博 查询");
        Subscription subFridens = weiboShowService.getFriends(oauth2AccessToken.getToken(),0,0,20,1,0,0,0)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Timber.e(" --> meth %s %s","getLastMyFriends", e.getMessage());
                    }

                    @Override
                    public void onNext(String o) {
                        Timber.i(" --> 微博 %s", o);
                    }
                });
    }

    @Override
    public void destroy() {
        //统一取消订阅,取消后 mCompositeSubscription
        mCompositeSubscription.unsubscribe();


    }
}
